package com.wangsd.web.shop.service;

import com.gitee.fastmybatis.core.query.Query;
import com.wangsd.common.basicbean.TreeData;
import com.wangsd.common.exception.BusinessException;
import com.wangsd.web.shop.entity.Customer;
import com.wangsd.web.shop.entity.Merchant;
import com.wangsd.web.shop.entity.Store;
import com.wangsd.web.shop.mapper.MerchantMapper;
import com.wangsd.web.shop.mapper.StoreMapper;
import com.wangsd.web.sys.entity.SysUser;
import com.wangsd.web.sys.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StoreService {
    @Resource
    private StoreMapper mapper;
    @Autowired
    private SysUserService userService;
    @Autowired
    private MerchantService merchantService;

    private Store selectByName(String name, Long id) {
        Query query = new Query();
        query.eq("name", name);
        if (id != null) {
            query.notEq("id", id);
        }
        return mapper.getByQuery(query);
    }

    public List<Store> selectPage(Store queryObj, String loginCode) {
        Query query = new Query();
        if (StringUtils.isNotBlank(queryObj.getName())) {
            query.like("name", queryObj.getName());
        }

        if (StringUtils.isNotBlank(loginCode)) {
            query.likeRight("code", loginCode);
        }

        if (StringUtils.isNotBlank(queryObj.getParentCode())) {
            query.likeRight("code", queryObj.getParentCode());
        }

        List<Store> list = mapper.list(query);
        return list;
    }


    public List<Store> selectByParentCode(String parentCode) {
        return mapper.listByColumn("parent_code", parentCode);
    }

    public int saveOrUpdate(Store entity) {
        Store store = this.selectByName(entity.getName(), entity.getId());
        if (store != null) {
            throw new BusinessException("名称不能重复！");
        }
        if (entity.getId() == null) {
            mapper.saveIgnoreNull(entity);
            entity.setCode(entity.getParentCode() + "-" + entity.getId());
        }
        return mapper.updateIgnoreNull(entity);
    }

    public int deleteById(Long id) {
        Store store = mapper.getById(id);
        List<SysUser> userList = userService.selectByParentCode(store.getCode());
        if (userList.size() > 0) {
            throw new BusinessException("商户下面有用户不允许删除！");
        }
        return mapper.deleteById(id);
    }

    public List<Store> selectAllStore(String loginCode) {
        Query query = new Query();
        if (StringUtils.isNotBlank(loginCode)) {
            query.likeRight("code", loginCode);
        }
        return mapper.list(query);
    }

    public List<TreeData> selectStoreTree(boolean isDisable, String loginCode) {
        //返回数据第一级list
        List<TreeData> dataList = merchantService.selectMerchantTree(isDisable, loginCode);
        List<Store> storeList = this.selectAllStore(loginCode);

        for (TreeData treeData : dataList) {
            if (treeData.getChildren() != null && treeData.getChildren().size() > 0) {
                //第二级list
                for (TreeData treeData2 : treeData.getChildren()) {
                    treeData2.setDisabled(isDisable);
                    Map<String, List<Store>> agentCollect = storeList.stream().collect(Collectors.groupingBy(Store::getParentCode));
                    List<Store> agents = agentCollect.get(treeData2.getValue());
                    if (agents != null) {
                        //第三级list
                        List agentArray = new ArrayList();
                        for (Store agent : agents) {
                            //第三级节点
                            TreeData agentObject = new TreeData(agent.getName(), agent.getCode());
                            agentArray.add(agentObject);
                        }
                        //添加第三级
                        treeData2.setChildren(agentArray);
                    }
                }
            }
        }
        return dataList;
    }

}
